package com.order.controller.buyer;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.order.pojo.ProductCategory;
import com.order.pojo.ProductInfo;
import com.order.service.IProductInfoService;
import com.order.service.ProductCategoryService;
import com.order.vo.ProductInfoVO;
import com.order.vo.ProductVO;
import com.order.vo.ResultVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/sell/buyer")
@Api(tags = "买家商品接口")
@Slf4j
public class BuyerProductController {

    @Autowired
    ProductCategoryService productCategoryService;

    @Autowired
    IProductInfoService productInfoService;

    @Autowired
    RedisTemplate redisTemplate;

    @GetMapping("product/list")
    public ResultVO<List<ProductVO>> getBuyerProductList() {

        List<ProductVO> productVOList = (List<ProductVO>)redisTemplate.opsForValue().get("productVOList");
        if(productVOList==null){
            productVOList = new ArrayList<>();
            List<ProductInfo> productInfoList = productInfoService.list(new QueryWrapper<ProductInfo>().eq("product_del", 0).eq("product_status", 0));
            Set<Integer> categoryTypeSet = new HashSet<>();
            for (ProductInfo productInfo : productInfoList) {
                categoryTypeSet.add(productInfo.getCategoryType());
            }
            List<ProductCategory> productCategoryList = productCategoryService.list(new QueryWrapper<ProductCategory>().eq("category_del", 0).in("category_id",categoryTypeSet));
            for (ProductCategory productCategory : productCategoryList) {
                ProductVO productVO = new ProductVO();
                productVO.setCategoryName(productCategory.getCategoryName());
                productVO.setCategoryType(productCategory.getCategoryType());
                List<ProductInfoVO> productInfoVOList = new ArrayList<>();
                for (ProductInfo productInfo : productInfoList) {
                    if(productInfo.getCategoryType().equals(productCategory.getCategoryId())){
                        ProductInfoVO productInfoVO = new ProductInfoVO();
                        productInfoVO.setProductId(productInfo.getProductId());
                        productInfoVO.setProductName(productInfo.getProductName());
                        productInfoVO.setProductPrice(productInfo.getProductPrice());
                        productInfoVO.setProductDescription(productInfo.getProductDescription());
                        productInfoVO.setProductIcon(productInfo.getProductIcon());
                        productInfoVOList.add(productInfoVO);
                    }
                }
                productVO.setProductInfoVOList(productInfoVOList);
                productVOList.add(productVO);
                log.info("从数据库中获取商品信息");
            }
            redisTemplate.opsForValue().set("productVOList", productVOList); //将结果存入Redis缓存
        }
        log.info("从缓存中获取商品信息");
        return new ResultVO<>(0,"成功",productVOList);
    }
}
